<template>
  <el-dialog v-model="dialogVisible" class="center" title="修改库存信息" width="400px" draggable>
    <el-form ref="ruleFormRef" :model="dialogProps.row" :rules="rules" label-width="100px">
      <el-form-item label="库存最大值" prop="maxHold">
        <el-input v-model="dialogProps.row!.maxHold" clearable></el-input>
      </el-form-item>
      <el-form-item label="库存预警值" prop="warnHold">
        <el-input v-model="dialogProps.row!.warnHold" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确定</el-button>
        <el-button @click="handleReset">重置</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import { Warehouse } from "@/api/interface";
import { FormInstance, ElMessage } from "element-plus";

const validateMaxHold = (rule: any, value: any, callback: any) => {
  const warnHoldValue = dialogProps.value.row.warnHold;
  if (warnHoldValue) {
    if (value - warnHoldValue <= 0) {
      callback(new Error("库存最大值必须大于库存预警值"));
    } else {
      callback();
    }
  }
};

const validateWarnHold = (rule: any, value: any, callback: any) => {
  const maxHoldValue = dialogProps.value.row.maxHold;
  if (maxHoldValue) {
    if (value - maxHoldValue >= 0) {
      callback(new Error("库存预警值必须小于库存最大值"));
    } else {
      callback();
    }
  }
};

const rules = reactive({
  maxHold: [
    { required: true, message: "请填写库存最大值" },
    { validator: validateMaxHold, trigger: "blur" }
  ],
  warnHold: [
    { required: true, message: "请填写库存预警值" },
    { validator: validateWarnHold, trigger: "blur" }
  ]
});

// 提交数据（更改）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await dialogProps.value.api!({
        inventoryId: dialogProps.value.row.inventoryId,
        maxHold: dialogProps.value.row.maxHold,
        warnHold: dialogProps.value.row.warnHold
      });
      ElMessage.success({ message: `库存信息修改成功` });
      dialogProps.value.getTableList!();
      dialogVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

interface DialogProps {
  title: string;
  isView: boolean;
  row: Partial<Warehouse.InventoryData>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const dialogVisible = ref(false);
const dialogProps = ref<DialogProps>({
  isView: false,
  title: "",
  row: {}
});
const openDialog = (params: DialogProps) => {
  dialogProps.value = params;
  dialogVisible.value = true;
  initialMaxHold = params.row.maxHold; // 获取初始的库存最大值
  initialWarnHold = params.row.warnHold; // 获取初始的库存预警值
};

let initialMaxHold: number | undefined; // 初始的库存最大值
let initialWarnHold: number | undefined; // 初始的库存预警值

// 重置按钮
const handleReset = () => {
  if (initialMaxHold !== undefined && initialWarnHold !== undefined) {
    dialogProps.value.row.maxHold = initialMaxHold; // 恢复初始的库存最大值
    dialogProps.value.row.warnHold = initialWarnHold; // 恢复初始的库存预警值
  }
};

defineExpose({ openDialog });
</script>
